<template>
  <div class="container">
    <div class="sbgl-nav breadcrumb">
      <img src="static/img/shu.png" alt="">
      <span>用户管理</span>
      <span class="glyphicon glyphicon-chevron-right"></span>
      <span>{{isAdd?'添加角色':'修改角色'}}</span>
    </div>
    <form class="form-horizontal" id="" @submit.prevent="submit">
      <div class="form-group" v-if="!isAdd">
          <label for="inputtext" class="col-sm-2 control-label col-sm-offset-3">角色ID：</label>
          <div class="col-sm-4">
            <input type="text" class="form-control jsgl-tab" id="inputtext" placeholder="" disabled :value="roleId">
          </div>
      </div>
      <div class="form-group">
          <label for="inputtext" class="col-sm-2 control-label col-sm-offset-3">角色名称：</label>
          <div class="col-sm-4">
            <input type="text" class="form-control jsgl-tab" id="inputtext" name="remoteip" value="" placeholder="角色名称" v-model="roleName">
          </div>
      </div>
      <div class="form-group">
          <label for="inputnumber2" class="col-sm-2 control-label col-sm-offset-3">权限：</label>
          <div class="col-sm-4 ">
            <div class="jsgl-tab-one">
              <table class="table table-bordered jsgl-tab-one-tab" id="tblDevice">
                <tbody> 
                  <tr v-for="(item, index) in privilegelist">
                    <td width="5%"><input type="checkbox" name="" v-model="selPrivilegeIds" :value="item.privilegeid"></td>
                    <td width="20%" class="list-one">{{item.privilegename}}</td>
                    <td width="20%" class="list-one">{{item.privilegeid}}</td>
                  </tr>
                </tbody>
              </table>
            </div>
          </div>
      </div>
      <p class="lx_btn">
        <button type="submit" class="btn btn-default btn-lg">保存</button>
        <button type="button" onclick="window.history.go(-1)" class="btn btn-default btn-lg">返回</button>
      </p>
    </form>   
  </div>
</template>

<script>
  import axios from 'axios'

  export default {
    name: 'addRole',
    data: function () {
      return {
        roleId: this.$route.params.roleId,
        privilegelist: [],
        method: this.$route.params.method || 'add',
        selPrivilegeIds: [],
        roleName: ''
      }
    },
    computed: {
      isAdd: function () {
        return this.method === 'add'
      },
      selArray: function () {
        var selArray = []
        for (var i in this.selPrivilegeIds) {
          var item = this.selPrivilegeIds[i]
          var selObj = this.privilegelist.find(function (o) {
            return o.privilegeid === item
          })
          selArray.push({
            privilegeid: selObj.privilegeid,
            privilegename: selObj.privilegename
          })
        }
        return selArray
      }
    },
    created: function () {
      if (!this.isAdd) {
        this.editInit()
      }
      this.privilegeInit()
    },
    methods: {
      submit: function () {
        var data = {
          method: 'AddRole',
          rolename: this.roleName,
          array: this.selArray
        }
        if (!this.isAdd) {
          data.method = 'UpdateRole'
          data.roleid = this.roleId
          data.rolename = this.roleName
        }
        this.$http.post('/api/hedacmdreq', data).then(response => {
          var body = response.data.HedaACK.Body
          window.duang.msg(true, body.retMsg)
        })
      },
      editInit: function () {
        var $this = this

        function getRolePrivilege () {
          return $this.$http.post('/api/hedacmdreq', {method: 'RolePrivilege', roleid: $this.roleId})
        }
        function getPrivilegeList () {
          return $this.privilegelist
        }
        // 统一请求，处理，必须要等2个请求全完成才能开始处理该数据
        axios.all([getRolePrivilege(), getPrivilegeList()]).then(axios.spread((myRes, res) => {
          var body = myRes.data.HedaACK.Body
          if (body.retCode === '0') {
            this.roleName = body.rolename
            var myList = body.privilegelist
            for (var i in myList) {
              var item = myList[i]
              this.selPrivilegeIds.push(item.privilegeid)
            }
          }
        }))
      },
      privilegeInit: function () {  // 所有权限初始化
        this.$http.post('/api/hedacmdreq', {method: 'GetPrivilege', flag: 0, privilege: ''}).then(response => {
          var body = response.data.HedaACK.Body
          this.privilegelist = body.privilegelist || []
        })
      }
    }
  }
</script>
